<?php
namespace app\port\controller;

use app\port\model\Questionway;
use app\port\model\Questiontype;
use app\port\model\Questioncourse;
use app\port\model\Question;
use think\Request;

class Questions extends Base{
	/**
	 * 分页返回升学方式列表
	 */
	public function getAllQuestionway(Request $request){
		$school_id = $request->post("schoolid");
		$school_pagesNumber  = $request->post("pageNumber")-1;
		$length  = $request->post("length");
		$total = ceil(Questionway::where('FIND_IN_SET(:school_id,school_id)',["school_id"=>$school_id])->count()/$length);
		$res = Questionway::where('FIND_IN_SET(:school_id,school_id)',["school_id"=>$school_id])->field("id,name")->limit($school_pagesNumber*$length,$length)->select();

		if($res){
		   return $this->request_json_total($res,$total,true);
        }else{
		   return $this->request_json($res);
        }
	}
    /**
     * 分页返回类别列表
     */
    public function getAllQuestiontype(Request $request){
        $school_id = $request->post("schoolid");
        $questionway_id = $request->post("questionwayid");
        $school_pagesNumber  = $request->post("pageNumber")-1;
        $length  = $request->post("length");
        $res = Questiontype::where('FIND_IN_SET(:school_id,school_id) and FIND_IN_SET(:questionway_id,questionway_id)',["school_id"=>$school_id,"questionway_id"=>$questionway_id])->field("id,name")->limit($school_pagesNumber*$length,$length)->select();
        $total = ceil( Questiontype::where('FIND_IN_SET(:school_id,school_id) and FIND_IN_SET(:questionway_id,questionway_id)',["school_id"=>$school_id,"questionway_id"=>$questionway_id])->count("id") / $length);

        return $this->request_json_total($res,$total,true);

    }
    /**
     * 分页返回类别列表
     */
    public function getAllQuestioncourse(Request $request){
        $school_id = $request->post("schoolid");
        $questionway_id = $request->post("questionwayid");
        $questiontype_id = $request->post("questiontypeid");
        $school_pagesNumber  = $request->post("pageNumber")-1;
        $length  = $request->post("length");
        $res = Questioncourse::where('FIND_IN_SET(:school_id,school_id) and FIND_IN_SET(:questionway_id,questionway_id) and FIND_IN_SET(:questiontype_id,questiontype_id)',["school_id"=>$school_id,"questionway_id"=>$questionway_id,"questiontype_id"=>$questiontype_id])->field("id,name")->limit($school_pagesNumber*$length,$length)->select();
        $total= ceil(Questioncourse::where('FIND_IN_SET(:school_id,school_id) and FIND_IN_SET(:questionway_id,questionway_id) and FIND_IN_SET(:questiontype_id,questiontype_id)',["school_id"=>$school_id,"questionway_id"=>$questionway_id,"questiontype_id"=>$questiontype_id])->count("id") / $length);
        if($res){
            return $this->request_json_total($res,$total,true);
        }else{
            return $this->request_json($res);
        }
    }
    /**
     * 分页查询题所有题目
     */
    public function getAllQuestion(Request $request){
        $school_id = $request->post("schoolid");
        $questionway_id = $request->post("questionwayid");
        $questiontype_id = $request->post("questiontypeid");
        $questioncourse_id = $request->post("questioncourseid");

        $school_pagesNumber  = $request->post("pageNumber")-1;
        $length  = $request->post("length");
        $res = Question::where(["status"=>1,"groupid"=>0])->where('FIND_IN_SET(:school_id,school_id) and FIND_IN_SET(:questionway_id,questionway_id) and FIND_IN_SET(:questiontype_id,questiontype_id) and FIND_IN_SET(:questioncourse_id,questioncourse_id)',["school_id"=>$school_id,"questionway_id"=>$questionway_id,"questiontype_id"=>$questiontype_id,"questioncourse_id"=>$questioncourse_id])->limit($school_pagesNumber*$length,$length)->order(["id"=>"asc"])->select();
        $total = ceil(Question::where(["status"=>1,"groupid"=>0])->where('FIND_IN_SET(:school_id,school_id) and FIND_IN_SET(:questionway_id,questionway_id) and FIND_IN_SET(:questiontype_id,questiontype_id) and FIND_IN_SET(:questioncourse_id,questioncourse_id)',["school_id"=>$school_id,"questionway_id"=>$questionway_id,"questiontype_id"=>$questiontype_id,"questioncourse_id"=>$questioncourse_id])->count("id") /$length);
        if($res){
            return $this->request_json_total($res,$total,true);
        }else{
            return $this->request_json($res);
        }
    }

    /**
     * 查询当前题目
     */
    public function getQuestion(Request $request){
        $id = $request->post("questionid");
        $school_id = $request->post("schoolid");
        $questionway_id = $request->post("questionwayid");
        $questiontype_id = $request->post("questiontypeid");
        $questioncourse_id = $request->post("questioncourseid");
        $res = Question::where(["id"=>$id,"status"=>1])->where('FIND_IN_SET(:school_id,school_id) and FIND_IN_SET(:questionway_id,questionway_id) and FIND_IN_SET(:questiontype_id,questiontype_id) and FIND_IN_SET(:questioncourse_id,questioncourse_id)',["school_id"=>$school_id,"questionway_id"=>$questionway_id,"questiontype_id"=>$questiontype_id,"questioncourse_id"=>$questioncourse_id])->limit(1)->find();
        if($res["isgroup"] == 1){
            $grop = Question::where(['groupid'=>$res['id']])->select();
            $res["group"]=$grop;
        }
        if($res){
            return $this->request_json($res,true);
        }else{
            return $this->request_json($res);
        }
    }

    /**
     * 查询上一题
     */
    public function getupQuestion(Request $request){
        $id = $request->post("questionid");
        $school_id = $request->post("schoolid");
        $questionway_id = $request->post("questionwayid");
        $questiontype_id = $request->post("questiontypeid");
        $questioncourse_id = $request->post("questioncourseid");

        $fid = Question::where(["status"=>1])->where('FIND_IN_SET(:school_id,school_id) and FIND_IN_SET(:questionway_id,questionway_id) and FIND_IN_SET(:questiontype_id,questiontype_id) and FIND_IN_SET(:questioncourse_id,questioncourse_id)',["school_id"=>$school_id,"questionway_id"=>$questionway_id,"questiontype_id"=>$questiontype_id,"questioncourse_id"=>$questioncourse_id])->limit(1)->order(["id"=>"asc"])->find();
//        echo Question::getLastSql(); //输出当前模型执行的sql语句

        if($id == $fid['id']){
            $res = Question::where(["id"=>$id,"status"=>1])->where('FIND_IN_SET(:school_id,school_id) and FIND_IN_SET(:questionway_id,questionway_id) and FIND_IN_SET(:questiontype_id,questiontype_id) and FIND_IN_SET(:questioncourse_id,questioncourse_id)',["school_id"=>$school_id,"questionway_id"=>$questionway_id,"questiontype_id"=>$questiontype_id,"questioncourse_id"=>$questioncourse_id])->limit(1)->find();

        }else{
            $res = Question::where(["id"=>["<",$id],"status"=>1])->where('FIND_IN_SET(:school_id,school_id) and FIND_IN_SET(:questionway_id,questionway_id) and FIND_IN_SET(:questiontype_id,questiontype_id) and FIND_IN_SET(:questioncourse_id,questioncourse_id)',["school_id"=>$school_id,"questionway_id"=>$questionway_id,"questiontype_id"=>$questiontype_id,"questioncourse_id"=>$questioncourse_id])->limit(1)->order(["id"=>"desc"])->find();
        }
        if($res['isgroup'] == 1){
            $grop = Question::where(['groupid'=>$res['id']])->select();
            $res["group"]=$grop;
        }
        if($res){
            return $this->request_json($res,true);
        }else{
            return $this->request_json($res);
        }
    }
    /**
     * 查询下一题
     */
    public function getdownQuestion(Request $request){
        $id = $request->post("questionid");
        $school_id = $request->post("schoolid");
        $questionway_id = $request->post("questionwayid");
        $questiontype_id = $request->post("questiontypeid");
        $questioncourse_id = $request->post("questioncourseid");
        $fid = Question::where(["status"=>1])->where('FIND_IN_SET(:school_id,school_id) and FIND_IN_SET(:questionway_id,questionway_id) and FIND_IN_SET(:questiontype_id,questiontype_id) and FIND_IN_SET(:questioncourse_id,questioncourse_id)',["school_id"=>$school_id,"questionway_id"=>$questionway_id,"questiontype_id"=>$questiontype_id,"questioncourse_id"=>$questioncourse_id])->limit(1)->order(["id"=>"desc"])->find();
        if($id ==$fid['id']){
            $res = Question::where(["id"=>$id,"status"=>1])->where('FIND_IN_SET(:school_id,school_id) and FIND_IN_SET(:questionway_id,questionway_id) and FIND_IN_SET(:questiontype_id,questiontype_id) and FIND_IN_SET(:questioncourse_id,questioncourse_id)',["school_id"=>$school_id,"questionway_id"=>$questionway_id,"questiontype_id"=>$questiontype_id,"questioncourse_id"=>$questioncourse_id])->limit(1)->find();
        }else{
            $res = Question::where(["id"=>[">",$id],"status"=>1])->where('FIND_IN_SET(:school_id,school_id) and FIND_IN_SET(:questionway_id,questionway_id) and FIND_IN_SET(:questiontype_id,questiontype_id) and FIND_IN_SET(:questioncourse_id,questioncourse_id)',["school_id"=>$school_id,"questionway_id"=>$questionway_id,"questiontype_id"=>$questiontype_id,"questioncourse_id"=>$questioncourse_id])->limit(1)->order(["id"=>"asc"])->find();

        }
        if($res['isgroup'] == 1){
            $grop = Question::where(['groupid'=>$res['id']])->select();
            $res["group"]=$grop;
        }
        if($res){
            return $this->request_json($res,true);
        }else{
            return $this->request_json($res);
        }
    }
}